package br.com.salaodebeleza.view;

import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;
import br.com.salaodebeleza.business.TbServicosBC;
import br.com.salaodebeleza.domain.TbServicos;

@ViewController
@NextView("./tbServicos_edit.jsf")
@PreviousView("./tbServicos_list.jsf")
public class TbServicosListMB extends AbstractListPageBean<TbServicos, Long> {

	private static final long serialVersionUID = 1L;
	
	@Inject
	private FacesContext facesContext;

	@Inject
	private TbServicosBC tbServicosBC;
	
	@Override
	protected List<TbServicos> handleResultList() {
		return this.tbServicosBC.findAll();
	}
	
	@Transactional
	public String deleteSelection() {
		boolean delete;
		try {
			for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
				Long id = iter.next();
				delete = getSelection().get(id);
				if (delete) {
					tbServicosBC.delete(id);
					iter.remove();
				}
			}
		} catch (Exception e) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Realizar Operacao! \nEntre em contato o Administrador do Sistema.", "");
			facesContext.addMessage(null, message);
		}
		
		return getPreviousView();
	}

}